<?php

namespace app\api\controller\web;


use app\admin\model\Org;
use app\admin\model\Role;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporder;
use app\admin\model\Shoproomset;
use app\admin\model\Shoptuan;
use app\admin\model\Shopwifi;
use app\admin\model\Tuanorder;
use app\common\controller\Api;
use OSS\OssClient;
use think\Config;

/**
 *
 */
class Device extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function doDevice(){

        $user = $this->auth->getUser();
        $deviceSn = $this->request->param('device_sn');
        $mobile = $this->request->param('mobile');
        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $deviceModel = new Shopdevice();
        $device = $deviceModel->where('device_sn',$deviceSn)->find();
        if($device['org_id'] != $org['id']) $this->error("设备归属不属于该品牌方，请联系技术");

        $shopModel = new \app\admin\model\Shop();
        $shop= $shopModel->where('mobile',$mobile)->where('org_id',$org['id'])->find();
        if(!$shop) $this->error("此手机号并不是店铺手机号，请把该号添加成店铺角色");

        $deviceModel->where('id',$device['id'])->update(['user_id'=>$shop['user_id']]);

    }


    public function getList()
    {
        $deviceId = $this->request->param('deviceId');
        $deviceSn = $this->request->param('deviceSn');

        $user = $this->auth->getUser();
        $shop_id = $this->request->param('shop_id');
        $page = $this->request->param('page');
        $limit = 10;
        $offset = ($page - 1) * $limit;

        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $where = array();
        $where['Shopdevice.org_id'] = $org['id'];

        if($shop_id != -1  && $shop_id != ""){
            $where['Shopdevice.shop_id'] =$shop_id;
        }
        if($deviceId != ""){
            $where['sn'] = $deviceId;
        }


        if($deviceSn != ""){
            $where['device_sn'] = ['like','%'.$deviceSn.'%'];
        }

        $shopDeviceModel = new \app\admin\model\Shopdevice();
        $list = $shopDeviceModel->with(['shop','room','user'])->limit($offset,$limit)->where($where)->select();
        $count = $shopDeviceModel->with(['shop'])->where($where)->count('Shopdevice.id');

        foreach ($list as $key => $val) {

            $list[$key]['shop_name'] = "";
            $list[$key]['room_name'] = "";
            $list[$key]['mobile'] = "";


            if($val['user'] != null){
                $list[$key]['mobile'] = $val['user']['mobile'];

            }

            if($val['shop'] != null){
                $list[$key]['shop_name'] = $val['shop']['name'];
            }
            if($val['room'] != null){
                $list[$key]['room_name'] = $val['room']['name'];
            }
            if($val['shop_id'] != -1 && $val['room_id'] == -1){
                $list[$key]['room_name'] = "大门";
            }
        }
        $ret = array();
        $ret['total'] =$count;
        $ret['list'] =$list;

        $this->success('请求成功', $ret);
    }



}
